package cn.spike.org.controller;

import cn.spike.basic.annotation.PreAuthorize;
import cn.spike.basic.util.AjaxResult;
import cn.spike.basic.util.PageList;
import cn.spike.org.domain.Department;
import cn.spike.org.query.DepartmentQuery;
import cn.spike.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:sea
 * @create:2023-01-12 10:59
 */
@Api(value = "部门的API",description = "处理部门的操作")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 接口：查询单个数据
     * @param id
     * @return
     */
    //前端：GET localhost:8080/department/1
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个数据")
    public Department loadOne(@PathVariable("id")Long id){
        return departmentService.loadById(id);
    }

    /**
     * 接口：查询所有
     * @return
     */
    //前端：GET localhost:8080/department
    @GetMapping
    @ApiOperation(value = "查询所有数据")
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
     * 接口：删除单条数据
     * @param id
     * @return
     */
    //前端：DELETE http://localhost:8080/department/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单条数据")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param department
     * @return
     */
    //前端：PUT http://localhost:8080/department json对象
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult save(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：分页和高级查询
     * @param departmentQuery
     * @return
     */
    //前端：POST http://localhost:8080/department json对象
    @PostMapping
    @ApiOperation(value = "分页和高级查询")
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery departmentQuery){
            return departmentService.pageQuery(departmentQuery);
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：获取部门树数据
     * @return
     */
    //前端：GET localhost:8080/department
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树数据")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }

}
